package com.it.controller.admin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.it.bean.R;
import com.it.utils.ConstantPropertiesUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("upload")
@Api(value = "v2", tags = "上传接口")
public class UploadController {


    @PostMapping("")
    @ApiOperation(value = "11", notes = "22")
    public R<?> upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();// 获取上传的文件名
        System.out.println("originalFilename = " + originalFilename);
        int i = originalFilename.lastIndexOf("."); // 查看最后一个出现.的位置
        String houzui = originalFilename.substring(i);  // 截取 这个索引开始也就是.开始的后缀
        UUID uuid = UUID.randomUUID();
        String url = ConstantPropertiesUtils.ym
                + "://" + ConstantPropertiesUtils.ip + ":" + ConstantPropertiesUtils.port +
                "/upload/" + uuid + houzui;
//        String rootFilePath = System.getProperty("user.dir") + "\\api\\src\\main\\resources\\upload\\" + uuid + houzui; // 上传到这
        String path = System.getProperty("user.dir") + "/img/" + uuid + houzui;
        System.out.println("rootFilePath = " + path);
        String property = System.getProperty("user.dir");
        System.out.println("property = " + property + "\\api\\main\\resouces\\upload");
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        return R.success("成功", url);
    }
}
